package es.tid.www.wsdl.parlayx.third_party_call.v1_1.consumer;

import es.tid.www.wsdl.parlayx.third_party_call.v1_1.PolicyException;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ServiceException;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.PolicyException13;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.ServiceException19;

public class ExceptionBuilder {
	
	/**
	 * builds a SOAP message related to an exception happened in the gateway service
	 * @param ex base exception with the information of the error
	 * @return the SOAP message to communicate.
	 */
	   public static ServiceException buildServiceException(Exception ex){
      	 String messageId="SVC0001";
      	 String text="A service error occurred. Error code is: ";
      	 String msg=ex.getMessage();
      	 if (msg.contains("SVC0261")){
      		messageId="SVC0261";
	    		text="Call session has already been terminated"; 
      	 }
      	 else if (msg.contains("SVC0002")){
      		messageId="SVC0002";
	    		text="Invalid input value for message part: ";
      	 }
      	 return buildServiceException(messageId,text,ex);
       }
       
	   /**
	    * builds a SOAP message related to an exception happened due to a policy fault in the webservice invocation
	    * @param ex base exception with the information of the error
	    * @return the SOAP message to communicate.
	    */
       public static PolicyException buildPolicyException(Exception ex){
      	 String messageId="POL0001";
      	 String text="A policy error ocurred. Error code is: ";
      	 String msg=ex.getMessage();
      	 if (msg.contains("POL1101")){
      		 messageId="POL1101";
      		 text="Too many participants";
      	 }
      	 else if (msg.contains("POL1102")){
      		 messageId="POL1102";
      		 text="Too few participants";
      	 }
      	 else if (msg.contains("POL0008")){
      		 messageId="POL0008";
      		 text="Charging not supported";
      	 }
      	 return buildPolicyException(messageId,text,ex);
       }
       
       /**
        * builds a SOAP message related to an exception happened in the gateway service
        * @param messageId identifier for the type of message
        * @param text indicates the origin of the exception
        * @param ex base complete exception with the information of the error
        * @return the SOAP message to communicate.
        */
       public static ServiceException buildServiceException(String messageId, String text, Exception ex){
    	   ServiceException19 se19=new ServiceException19();
    	   ThirdPartyCallServiceStub.ServiceException stubSe=new ThirdPartyCallServiceStub.ServiceException();
    	   stubSe.setMessageId(messageId);
    	   stubSe.setText(text);
    	   stubSe.setVariables(new String[]{ex.getMessage()});
    	   ServiceException se=new ServiceException(ex.getMessage());
    	   se19.setServiceException(stubSe);
    	   se.setFaultMessage(se19);
    	   return se;  
       }
       
       
       /**
        * builds a SOAP message related to an exception happened due to a policy fault in the webservice invocation
        * @param messageId identifier for the type of message
        * @param text indicates the origin of the exception
        * @param ex base complete exception with the information of the error
        * @return the SOAP message to communicate.
        */
       public static PolicyException buildPolicyException(String messageId, String text, Exception ex){
      	 ThirdPartyCallServiceStub.PolicyException innerException=
	    			new ThirdPartyCallServiceStub.PolicyException();
      	 innerException.setMessageId(messageId);
      	 innerException.setText(text);
      	 innerException.setVariables(new String[]{ex.getMessage()});
      	 PolicyException13 pe3=new PolicyException13();
      	 pe3.setPolicyException(innerException);
      	 PolicyException pe=new PolicyException(ex.getMessage());
      	 pe.setFaultMessage(pe3);
      	 return pe;
       }

}
